package com.qilu.pe.ui.activity;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.qilu.pe.R;
import com.qilu.pe.base.Base2Activity;
import com.qilu.pe.base.BaseResult2;
import com.qilu.pe.base.Global;
import com.qilu.pe.dao.bean.Patient;
import com.qilu.pe.dao.http.APIRetrofit;
import com.qilu.pe.support.util.GlideUtil;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.arad.base.base_list.SuperViewHolder;

import java.util.ArrayList;
import java.util.List;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

// 患者列表
public class PatientListActivity extends Base2Activity {
    private RecyclerView rcv_list;// 患者列表

    private PatientListAdapter adapter;
    private List<Patient> patientList = new ArrayList<>();

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_patient_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_list = findViewById(R.id.rcv_list);

        setHeadTitle("患者列表");

        adapter = new PatientListAdapter(this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);

        setListeners();

        hideProgress();
        reqRecentPatientList();
        showProgress();
    }

    private void setListeners() {
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putSerializable("patient", patientList.get(position));
                startActivity(PatientDetailActivity.class, bundle);
            }
        });
    }

    // 请求近期患者列表
    private Disposable reqRecentPatientList() {
        return APIRetrofit.getDefault().reqRecentPatientList(Global.HEADER, 1, 999999, 0)
                .compose(RxUtil.<BaseResult2<List<Patient>>>subIoObMain())
                .subscribe(new Consumer<BaseResult2<List<Patient>>>() {
                    @Override
                    public void accept(BaseResult2<List<Patient>> result2) throws Exception {
                        hideProgress();
                        if (result2.isSuccess()) {

                        } else {

                        }
                        patientList = result2.getData();
                        adapter.setDataList(patientList);
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        hideProgress();
                        throwable.printStackTrace();
                    }
                });
    }

    private class PatientListAdapter extends ListBaseAdapter<Patient> {

        public PatientListAdapter(Context context) {
            super(context);
        }

        @Override
        public int getLayoutId() {
            return R.layout.item_patient_list_2;
        }

        @Override
        public void onBindItemHolder(SuperViewHolder holder, int position) {
            RoundedImageView iv_header = holder.getView(R.id.iv_header);
            ImageView iv_sex = holder.getView(R.id.iv_sex);
            TextView tv_name = holder.getView(R.id.tv_name);
            TextView tv_phone = holder.getView(R.id.tv_phone);
            TextView tv_sex = holder.getView(R.id.tv_sex);
            TextView tv_age = holder.getView(R.id.tv_age);
            TextView tv_order = holder.getView(R.id.tv_order);

            final Patient patient = mDataList.get(position);

            GlideUtil.loadImg(patient.getPicture(), iv_header);
            iv_sex.setImageResource(TextUtils.equals("1", patient.getSex()) ?
                    R.mipmap.ic_patient_sex_female : R.mipmap.ic_patient_sex_male);// ic_patient_sex_female ic_patient_sex_male
            tv_name.setText(patient.getName());
            tv_phone.setText(patient.getPhone());
            tv_sex.setText(TextUtils.equals("1", patient.getSex()) ? "女" : "男");
            tv_age.setText(patient.getBirth());

            // 立即预约
            tv_order.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("patient", patient);
                    startActivity(PlaceOrderActivity.class, bundle);
                }
            });
        }
    }
}